/**
 * @Date 2023-02-02 16:13:11
 * @Remark
 */

// lib
// config
// script & methods & public
import toast from "components/toast";
// store
import { statePermissionModal } from "./state";
// service
import {
  editRolePermissionService,
  getRolePermissionService,
} from "biz/Role/service";
// interface
// 其它

export async function getRolePermission(model: RoleDto) {
  statePermissionModal.targetKeys = [];
  // 调用服务
  const result = await getRolePermissionService(model.id);
  // 成功
  if (result.payload) {
    const list = result.payload.map((el) => el.id);
    statePermissionModal.targetKeys = list;
  }
}

export async function editRolePermission(value: string[]) {
  const role = statePermissionModal.getModal();
  if (!role) return;
  // 开始前
  statePermissionModal.modalLoading = true;
  // 调用服务
  const result = await editRolePermissionService(
    role.id,
    statePermissionModal.getTargetKeys(),
    value,
  );
  statePermissionModal.modalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("设置成功");
    getRolePermission(role)
  }
}

export async function showPermissionModal(model: RoleDto) {
  statePermissionModal.modal = model;
  getRolePermission(model);
}
